# project.py

from ot_simulator.ordered_list import ordered_list
from ot_simulator.setup import setup

class project(object):
    r'''One per project.

    This is what File -> Open reads in.
    '''
    def __init__(self, name, *pieces):
        self.name = name
        if not pieces:
            self.pieces = ordered_list((piece('piece_1'),))
        else:
            self.pieces = ordered_list(pieces)

class piece(object):
    r'''One per piece (or part).
    '''
    def __init__(self, name, *setups):
        self.name = name
        if not setups:
            self.setups = ordered_list((setup('setup_1'),))
        else:
            self.setups = ordered_list(setups)
        self.index = 0
        self.comments = []

    def add_comments(self, *comments):
        self.comments.extend(comments)

    def run(self, type_of_run, dest_machine, filename):
        dest_machine.name = self.name
        dest_machine.add_comments(self.comments)
        dest_machine.open(filename)
        self.run2(type_of_run, dest_machine)

    def run2(self, type_of_run, dest_machine, start_index=0):
        for su in self.setups[start_index:]:
            su.run(type_of_run, dest_machine)
            self.index += 1
        dest_machine.close()

    def cont(self, type_of_run, dest_machine, filename):
        self.setups[self.index].cont(type_of_run, dest_machine)
        self.index += 1
        self.run2(type_of_run, dest_machine, self.index)

